package com.genuine.vip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.genuine.hyjj.AssistUser;
import com.genuine.hyjj.Generate_qrcode_Detail;
import com.genuine.hyjj.R;
import com.genuine.hyjj.Youhuiquan;
import com.genuine.net.ApacheHttpConnect;

public class VipCardListActivity extends Activity {
	private String uid = "";
	private ProgressDialog dialog = null;
	private List<HashMap<String, Object>> list;
	private ListView listview = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_vip_card_list);
		uid = AssistUser.getInstance(this).getId();
		listview = (ListView) findViewById(R.id.lv_vipCard);
		list = new ArrayList<HashMap<String, Object>>();
		GetCardListTask task = new GetCardListTask();
		task.execute(uid);
	}

	class GetCardListTask extends AsyncTask<String, Integer, String> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = ProgressDialog.show(VipCardListActivity.this, "获取会员卡",
					"正在从服务器获取信息...");
			dialog.show();
		}

		protected void onPostExecute(String result) {
			dialog.dismiss();
			Log.d("demo", result);
			if (result.equals("-1")) {
				// Log.d("demo", "服务器错误或参数无效!");
				Toast.makeText(VipCardListActivity.this, "你目前还没有会员卡",
						Toast.LENGTH_LONG).show();
			} else {
				try {
					/*
					 * [{"CardNo":"C9967000007","RuleDetail":"7折优惠","BeginDate":
					 * "2012-5-22 11:37:08",
					 * "EndDate":"2012-12-30 0:00:00","CommodityName"
					 * :"御膳阁牌灵芝孢子粉", "FactoryName":"无锡御膳阁生物科技有限公司"}]
					 */
					JSONArray objs = new JSONArray(result);
					Log.d("demo", objs.length() + "");
					for (int i = 0; i < objs.length(); i++) {
						JSONObject jobj = objs.getJSONObject(i);
						HashMap<String, Object> hashmap = new HashMap<String, Object>();
						hashmap.put("CardNo", jobj.getString("CardNo"));
						hashmap.put("RuleDetail", jobj.getString("RuleDetail"));
						hashmap.put("BeginDate", jobj.getString("BeginDate"));
						hashmap.put("EndDate", jobj.getString("EndDate"));
						hashmap.put("CommodityName",
								jobj.getString("CommodityName"));
						hashmap.put("FactoryName",
								jobj.getString("FactoryName"));
						list.add(hashmap);
					}
					SimpleAdapter adapter = new SimpleAdapter(
							VipCardListActivity.this, list,
							R.layout.my_vip_card_list_item, new String[] {
									"CardNo", "FactoryName" }, new int[] {
									R.id.vip_item_cardNo,
									R.id.vip_item_factoryName });
					listview.setAdapter(adapter);

					AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							ListView listView = (ListView) parent;
							HashMap<String, String> map = (HashMap<String, String>) listView
									.getItemAtPosition(position);
							Intent intent = new Intent();
							intent.putExtra("d", map);
							intent.setClass(VipCardListActivity.this,
									VipCardDetail.class);
							startActivity(intent);
						}

					};
					if (list.size() <= 0) {
						Toast.makeText(VipCardListActivity.this, "你目前还没有会员卡",
								Toast.LENGTH_LONG).show();
					}
					listview.setOnItemClickListener(listener);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		protected String doInBackground(String... params) {
			String url = "http://www.ypdm.com/Handler/Vip/GetVipCardByUid.ashx?uid="
					+ params[0];

			String result = null;
			try {
				result = ApacheHttpConnect.getContent(url);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return result.trim();
		}
	}
}
